package person.aoao.library.controller;


import org.springframework.web.bind.annotation.*;
import person.aoao.library.dao.BorrowListDao;
import person.aoao.library.dao.CommentListDao;
import person.aoao.library.dao.WillListDao;
import person.aoao.library.entity.BookList;
import person.aoao.library.entity.BorrowList;
import person.aoao.library.entity.Collect;
import person.aoao.library.entity.CommentList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.*;
import person.aoao.library.vo.BookNav;
import person.aoao.library.vo.ListData;
import person.aoao.library.vo.SeatListPlus;
import person.aoao.library.vo.TimePlus;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("app")
@CrossOrigin
public class AppController {
    @Resource
    private BannerListService bannerListService;
    @Resource
    private AddressService addressService;

    @Resource
    private BookListService bookListService;

    @Resource
    private BorrowListDao borrowListDao;

    @Resource
    private CommentListDao commentListDao;
    @Resource
    private BorrowListService borrowListService;

    @Resource CollectService collectService;
    @Resource
    private CommentListService commentListService;

    @Resource
    private WillListService willListService;

    @Resource
    private WillListDao willListDao;

    @Resource
    private SeatListService seatListService;

    /*
        获取轮播图
     */
    @GetMapping("getBannerOnApp")
    public ResponseData getBanner(){
        ResponseData data=this.bannerListService.getBannerOnApp();
        return data;
    }

    /*
    获取打卡地址
    */
    @GetMapping("getAddressOnApp")
    public ResponseData getAddress(){
        ResponseData data=this.addressService.getAddressOnApp();
        return data;
    }

    /*
    * 类型分类
    * */
    @GetMapping("getnavList/{type}/{num}/{page}")
    public ResponseData getAddress(@PathVariable Integer type,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.bookListService.getNavBook(type,num,page);
        return  data;
    }

    @GetMapping("getBookOne/{bookid}")
    public ResponseData getBookOne(@PathVariable Integer bookid){
        BookList data=this.bookListService.queryById(bookid);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @GetMapping("getBorrowOne/{uid}/{bookid}")
    public ResponseData getBorrowOne(@PathVariable String uid,@PathVariable Integer bookid){
        return new ResponseData(ResponseCode.SUCCESS,this.borrowListDao.getStateByUser(uid,bookid));
    }

    @GetMapping("getBookComments/{bookid}/{num}/{page}")
    public ResponseData getComments(@PathVariable Integer bookid,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.commentListService.getComments(bookid,num,page);
        return data;
    }

    @PostMapping("addCommentByBook")
    public ResponseData addComments(@RequestBody CommentList commentList){
        return new ResponseData(ResponseCode.SUCCESS,this.commentListService.insert(commentList));
    }

    @GetMapping("getBorrowListByUid/{uid}/{num}/{page}")
    public ResponseData getBorrowList(@PathVariable String uid,@PathVariable Integer num,@PathVariable Integer page){
        ListData data=this.borrowListService.getBorrowList(uid,num,page);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @PostMapping("addBookBorrow")
    public ResponseData addBorrow(@RequestBody BorrowList borrowList){
        borrowList.setState(1);
        return new ResponseData(ResponseCode.SUCCESS,this.borrowListService.insert(borrowList));
    }

    @GetMapping("getCommentsByUid/{uid}/{num}/{page}")
    public ResponseData getCommentsByUid(@PathVariable String uid,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.commentListService.getCommentsByUid(uid,num,page);
        return data;
    }

    @PostMapping("deleteComment")
    public ResponseData deleteComment(@RequestBody CommentList commentList){
        return new ResponseData(ResponseCode.SUCCESS,this.commentListService.deleteById(commentList.getCid()));
    }

    @GetMapping("getNavCount/{uid}")
    public ResponseData getNavCount(@PathVariable String uid){
        BookNav borror=new BookNav();
        borror.setNav("图书借阅");
        borror.setAllCount(this.borrowListDao.countByUid(uid));
        borror.setUseCount(this.borrowListDao.countByUidState(uid));
        BookNav wiil=new BookNav();
        wiil.setNav("座位预约");
        wiil.setAllCount(this.willListDao.getCountByUid(uid));
        wiil.setUseCount(this.willListDao.getCountByUidState(uid));
        BookNav comment=new BookNav();
        comment.setNav("我的评论");
        comment.setAllCount(this.commentListDao.getCountByUid(uid));
        List<BookNav> data=new ArrayList<>();
        data.add(borror);
        data.add(wiil);
        data.add(comment);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @GetMapping("searchWillListByUid/{uid}/{state}/{num}/{page}")
    public ResponseData searchWillListByUid(@PathVariable String uid,@PathVariable Integer state,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.willListService.getWillListByUidState(uid,state,num,page);
        return data;
    }

    @GetMapping("getCollection/{uid}/{num}/{page}")
    public ResponseData getCollection(@PathVariable String uid,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.collectService.getCollectList(uid,num,page);
        return  data;
    }

    @PostMapping("addCollection")
    public ResponseData addCollection(@RequestBody Collect collect){
        return new ResponseData(ResponseCode.SUCCESS,this.collectService.insert(collect));
    }

    @GetMapping("isCollect/{uid}/{bookid}")
    public ResponseData isCollect(@PathVariable String uid,@PathVariable Integer bookid){
        ResponseData data=this.collectService.isCollect(uid,bookid);
        return  data;
    }

    @PostMapping("deleteCollect")
    public ResponseData deleteCollect(@RequestBody ArrayList<Integer> list){
        System.out.println(list);
        for(Integer i :list){
            this.collectService.deleteById(i);
        }
        return new ResponseData(ResponseCode.SUCCESS,true);
    }


    @PostMapping("getSeatListPlus")
    public ResponseData getSeatListPlus(@RequestBody SeatListPlus qurey){
        System.out.println(qurey.getRetruntime());
        ResponseData data=this.seatListService.getSeatListPlus(qurey);
        return data;
    }

    @PostMapping("getTimePlus")
    public ResponseData getTimePlus(@RequestBody TimePlus queey){
        System.out.println(queey.getSeatid());
        ResponseData data =this.willListService.getTimePlus(queey);
        return  data;
    }

}
